package com.meituan.sqt.mock.infrastructure.persistence.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.mapper.TypeCPayInfoMapper;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.mapper.TypeCRefundInfoMapper;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.po.TypeCPayInfoPo;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.po.TypeCRefundInfoPo;
import com.meituan.sqt.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: chenzhenfeng@meituan.com
 * @date: 2023/1/30 16:50
 * @description:
 */
@Service
@Slf4j
public class TypeCRepository {
    @Resource
    private TypeCPayInfoMapper payInfoMapper;
    @Resource
    private TypeCRefundInfoMapper refundInfoMapper;

    // 支付相关
    public TypeCPayInfoPo queryByTradeNo(Long tradeNo) {
        Preconditions.checkNotNull(tradeNo);
        QueryWrapper<TypeCPayInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("trade_no", tradeNo);
        return payInfoMapper.selectOne(queryWrapper);
    }

    public void savePayInfo(TypeCPayInfoPo payInfoPO) {
        payInfoMapper.insert(payInfoPO);
        log.info("保存支付记录, payInfo:{}", JsonUtil.object2Json(payInfoPO));
    }

    // 退款相关
    public TypeCRefundInfoPo queryByRefundTradeNo(Long refundTradeNo) {
        Preconditions.checkNotNull(refundTradeNo);
        QueryWrapper<TypeCRefundInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund_trade_no", refundTradeNo);
        return refundInfoMapper.selectOne(queryWrapper);
    }

    public List<TypeCRefundInfoPo> listRefundInfosByPayId(Long payId) {
        QueryWrapper<TypeCRefundInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_id", payId);
        return refundInfoMapper.selectList(queryWrapper);
    }

    public void saveRefundInfo(TypeCRefundInfoPo refundInfoPo) {
        refundInfoMapper.insert(refundInfoPo);
        log.info("保存退款记录, refundInfo:{}", JsonUtil.object2Json(refundInfoPo));
    }
}
